package com.dream.dao.impl;

import com.dream.dao.VisitInformationDao;
import com.dream.pojo.VisitInformation;
import com.dream.utils.DBUtil;

import java.util.List;

public class VisitInformationDaoImpl implements VisitInformationDao {
    @Override
    public long totalCount(String stuName, Long dormitoryId) {
        String sql = "SELECT\n" +
                "\tcount(visit_record.stu_id)\n" +
                "FROM\n" +
                "  visit_record\n" +
                "INNER JOIN dormitory ON visit_record.dormitory_id = dormitory.dormitory_id\n" +
                "INNER JOIN student ON student.stu_id = visit_record.stu_id\n" +
                "INNER JOIN clazz ON clazz.clazz_id=student.clazz_id\n" +
                "INNER JOIN major ON clazz.major_id = major.major_id\n" +
                "INNER JOIN department ON department.dept_id = major.dept_id  where 1=1";
        StringBuilder sb = new StringBuilder(sql);
        if (stuName!=null&&!"".equals(stuName)){
            sb.append(" and student.stu_name like '%"+stuName+"%'");
        }
        if(dormitoryId!=null && !"-1".equals(dormitoryId.toString())){
            sb.append(" and visit_record.dormitory_id="+dormitoryId);
        }
        System.out.println(sb.toString());
        return DBUtil.queryCount(sb.toString());
    }

    @Override
    public List<VisitInformation> list(String stuName, Long dormitoryId, long pageIndex, long pageSize) {
        String sql = "SELECT\n" +
                "\tstudent.stu_id,student.stu_name,department.dept_name,major.major_name,clazz.clazz_name,\n" +
                "dormitory.dormitory_name,visit_record.stu_temp\n" +
                "  ,visit_record.record_date\n" +
                "FROM\n" +
                "  visit_record\n" +
                "INNER JOIN dormitory ON visit_record.dormitory_id = dormitory.dormitory_id\n" +
                "INNER JOIN student ON student.stu_id = visit_record.stu_id\n" +
                "INNER JOIN clazz ON clazz.clazz_id=student.clazz_id\n" +
                "INNER JOIN major ON clazz.major_id = major.major_id\n" +
                "INNER JOIN department ON department.dept_id = major.dept_id  where 1=1";
        StringBuilder sb = new StringBuilder(sql);
        if(stuName!=null && !"".equals(stuName)){
            sb.append(" and student.stu_name like '%"+stuName+"%'");
        }
        if(dormitoryId!=null && !"-1".equals(dormitoryId.toString())){
            sb.append(" and visit_record.dormitory_id="+dormitoryId);
        }
        sb.append(" limit ?,?");
        return DBUtil.commonQuery(VisitInformation.class, sb.toString(),(pageIndex-1)*pageSize,pageSize);
    }
}
